<template>
  <div class="container" :style="'height: ' + docheight + 'px'">
    <leftcont></leftcont>
    <div class="righCont">
      <topcont></topcont>
      <div class="cardCont">
        <div class="carItem">
          <div class="carName">总计报名金额</div>
          <div class="carText">{{ allData.total_money }}元</div>
        </div>
        <div class="carItem">
          <div class="carName">报名人数（学生）</div>
          <div class="carText">{{ allData.sign_up_student_num }}人</div>
        </div>
        <div class="carItem">
          <div class="carName">报名人数（陪同）</div>
          <div class="carText">{{ allData.sign_up_parents_num }}人</div>
        </div>
      </div>
      <div class="content">
        <div class="payTable">
          <div class="trendTop">
            <div>报名列表({{ total }})</div>
          </div>
          <div class="tableCont">
            <div class="tableSerchCont">
              <div class="serchItem">
                <el-select v-model="pay_status" placeholder="请选择会员状态">
                  <el-option
                    v-for="(item, index) in navArr"
                    :key="index"
                    :label="item.val"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </div>
              <div class="serchItem" style="width: 250px;">
                <el-input
                  v-model="signName"
                  placeholder="报名人姓名"
                ></el-input>
              </div>
              <div class="serchItem">
                <btn @click="serchFn" value="搜索"></btn>
              </div>
              <div class="serchItem">
                <btn @click="excelFn" value="导出excel表"></btn>
              </div>
            </div>
            <el-table :data="orderList" style="width: 100%">
              <el-table-column
                prop="order_number"
                width="200"
                show-overflow-tooltip
                label="订单号"
              >
              </el-table-column>
              <el-table-column
                prop="realname"
                show-overflow-tooltip
                label="姓名"
              >
              </el-table-column>
              <el-table-column show-overflow-tooltip label="付款状态">
                <template slot-scope="scope">
                  <div class="ctrolBtn">
                    <span class="redBtn" v-if="scope.row.pay_status == 1"
                      >未支付</span
                    >
                    <span class="greenBtn" v-if="scope.row.pay_status == 2"
                      >已支付</span
                    >
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="sex" show-overflow-tooltip label="性别">
                <template slot-scope="scope">
                  <div>{{ scope.row.sex == 1 ? "男生" : "女生" }}</div>
                </template>
              </el-table-column>
              <el-table-column
                prop="class_name"
                show-overflow-tooltip
                label="年级班级"
              >
              </el-table-column>
              <el-table-column
                prop="signNum"
                show-overflow-tooltip
                label="报名人数"
                width="150"
              >
                <template slot-scope="scope">
                  <div>
                    {{ scope.row.sign_up_student_num }}学生{{
                      scope.row.sign_up_parents_num
                    }}陪同(共{{
                      scope.row.sign_up_student_num +
                        scope.row.sign_up_parents_num
                    }}人)
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                prop="bill_money"
                show-overflow-tooltip
                label="报名费用"
              >
              </el-table-column>
              <el-table-column
                prop="cf_time"
                show-overflow-tooltip
                label="出发日期"
              >
              </el-table-column>
              <el-table-column
                prop="mobile"
                show-overflow-tooltip
                label="联系电话"
              >
              </el-table-column>
              <el-table-column width="200" label="操作">
                <template slot-scope="scope">
                  <div class="ctrolBtn">
                    <div
                      class="text_btn blueBtn hasRight"
                      @click="checkFn(scope.row)"
                    >
                      check
                    </div>
                    <div
                      class="text_btn greenBtn hasRight"
                      @click="detailFn(scope.row)"
                    >
                      详情
                    </div>
                    <div
                      class="text_btn balckBtn"
                      v-if="scope.row.autograph != ''"
                      @click="lookSign(scope.row)"
                    >
                      查看合同
                    </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <div class="pageCont">
              <el-pagination
                background
                layout="prev, pager, next"
                :page-size="per_page"
                @current-change="pageFn"
                :current-page="page"
                :total="total"
              >
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="报名完整信息" :visible.sync="dialogVisible" width="60%">
      <div class="formInfo">
        <div class="formItem">
          <div class="formName">订单号</div>
          <div class="formVal">{{ activeItem.order_number }}</div>
        </div>
        <div class="formItem">
          <div class="formName">付款状态</div>
          <div class="formVal">
            <span class="redBtn" v-if="activeItem.pay_status == 1">未支付</span>
            <span class="greenBtn" v-if="activeItem.pay_status == 2"
              >已支付</span
            >
          </div>
        </div>
        <div class="formItem">
          <div class="formName">报名项目</div>
          <div class="formVal">
            {{
              activeItem.bm_type == 1 ? "新加坡研学冬令营" : "上海交大冬令营"
            }}
          </div>
        </div>
        <div class="formItem">
          <div class="formName">学生姓名</div>
          <div class="formVal">{{ activeItem.realname }}</div>
        </div>
        <div class="formItem">
          <div class="formName">学生性别</div>
          <div class="formVal">{{ activeItem.sex == 1 ? "男生" : "女生" }}</div>
        </div>
        <div class="formItem">
          <div class="formName">年级班级</div>
          <div class="formVal">{{ activeItem.class_name }}</div>
        </div>
        <div class="formItem">
          <div class="formName">报名人数</div>
          <div class="formVal">
            {{ activeItem.sign_up_student_num }}学生{{
              activeItem.sign_up_parents_num
            }}陪同(共{{
              activeItem.sign_up_parents_num + activeItem.sign_up_student_num
            }}人)
          </div>
        </div>
        <div class="formItem">
          <div class="formName">报名费用</div>
          <div class="formVal">{{ activeItem.bill_money }}</div>
        </div>
        <div class="formItem">
          <div class="formName">出发日期</div>
          <div class="formVal">{{ activeItem.cf_time }}</div>
        </div>
        <div class="formItem">
          <div class="formName">联系电话</div>
          <div class="formVal">{{ activeItem.mobile }}</div>
        </div>
        <div class="formItem">
          <div class="formName">学生年龄</div>
          <div class="formVal">{{ getAgeByIDCard(activeItem.id_card) }}</div>
        </div>
        <div class="formItem">
          <div class="formName">身份证号</div>
          <div class="formVal">{{ activeItem.id_card }}</div>
        </div>
        <div class="formItem">
          <div class="formName">出生日期</div>
          <div class="formVal">{{ activeItem.date_birth }}</div>
        </div>
        <div class="formItem">
          <div class="formName">学生民族</div>
          <div class="formVal">{{ activeItem.mz }}</div>
        </div>
        <div class="formItem">
          <div class="formName">所属国籍</div>
          <div class="formVal">{{ activeItem.gj }}</div>
        </div>
        <div class="formItem">
          <div class="formName">身体状况</div>
          <div class="formVal">{{ activeItem.stzk }}</div>
        </div>
        <div class="formItem">
          <div class="formName">报名时间</div>
          <div class="formVal">{{ activeItem.create_time }}</div>
        </div>
        <div class="formItem">
          <div class="formName">支付时间</div>
          <div class="formVal">{{ activeItem.pay_time }}</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import leftcont from "@/components/leftCont.vue";
import topcont from "@/components/topCont.vue";
import btn from "@/components/public/btn.vue";
import { statistics, admin, excel, checkStatus } from "@/http/api";
import * as XLSX from "xlsx";
import { saveAs } from "file-saver";
export default {
  components: { leftcont, topcont, btn },
  data() {
    return {
      dialogVisible: false,
      orderList: [],
      allData: {
        allMoney: 278319,
        parentNum: 79,
        childNum: 140
      },
      total: 0,
      signName: "",
      page: 1,
      per_page: 5,
      excelArr: [],
      activeItem: {},
      pay_status: "",
      navArr: [
        { val: "全部", id: "" },
        { val: "未支付", id: 1 },
        { val: "已支付", id: 2 }
      ]
    };
  },
  props: {},
  methods: {
    lookSign(item) {
      if (item.autograph == "") {
        this.$message("该用户未签名");
        return false;
      }
      this.$router.push({
        path: "/userCenterOrderSign",
        query: { sign_up_id: item.sign_up_id, realname: item.realname }
      });
    },
    pageFn(index) {
      this.page = index;
      this.getData();
    },

    getAgeByIDCard(idCard) {
      if (!idCard) {
        return false;
      }
      console.log(idCard);
      const birthYear = idCard.substring(6, 10);
      const currentYear = new Date().getFullYear();
      const age = currentYear - parseInt(birthYear);
      return age;
    },
    serchFn() {
      this.orderList = [];
      this.page = 1;
      this.getData();
    },
    detailFn(item) {
      this.dialogVisible = true;
      this.activeItem = item;
    },
    getData() {
      let data = {
        search: this.signName,
        per_page: this.per_page,
        page: this.page,
        pay_status: this.pay_status
      };
      admin(data)
        .then(res => {
          if (res.data.code == 1) {
            this.orderList = res.data.data.data;
            this.total = res.data.data.total;
          } else {
            this.$message(res.data.msg);
          }
        })
        .catch(err => {
          this.$message("服务器请求超时");
        });
    },
    getHeadData() {
      let data = {};
      statistics(data)
        .then(res => {
          if (res.data.code == 1) {
            this.allData = res.data.data;
          } else {
            this.$message(res.data.msg);
          }
        })
        .catch(err => {
          this.$message("服务器请求超时");
        });
    },
    excelFn() {
      let data = {};
      excel(data)
        .then(res => {
          if (res.data.code == 1) {
            if (res.data.data.length == 0) {
              this.$message("暂无数据可导出");
              return false;
            }
            this.excelArr = res.data.data;
            for (let i = 0; i < this.excelArr.length; i++) {
              this.excelArr[i].sex =
                this.excelArr[i].sex == 1 ? "男生" : "女生";
              this.excelArr[i].pay_status =
                this.excelArr[i].pay_status == 1 ? "未支付" : "已支付";
            }
            let arr = this.excelArr;
            let headArr = [
              "订单号",
              "姓名",
              "性别",
              "班级",
              "学生人数",
              "陪同人数",
              "出发日期",
              "联系电话",
              "护照号码",
              "身份证号",
              "出生日期",
              "民族",
              "国籍",
              "身体状况",
              "报名费用",
              "付款状态",
              "支付时间",
              "报名时间"
            ];
            let exportArr = [headArr];
            for (let i = 0; i < arr.length; i++) {
              let itemArr = [];
              for (let a in arr[i]) {
                itemArr.push(arr[i][a]);
              }
              exportArr.push(itemArr);
            }
            this.exportToExcel(exportArr);
          } else {
            this.$message(res.data.msg);
          }
        })
        .catch(err => {
          this.$message("服务器请求超时");
        });
    },
    exportToExcel(data) {
      console.log(data);
      // 将数据转换为工作表
      const worksheet = XLSX.utils.aoa_to_sheet(data);

      // 创建工作簿并添加工作表
      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
      // 生成Excel文件
      const excelBuffer = XLSX.write(workbook, {
        bookType: "xlsx",
        type: "array"
      });
      // 使用blob和saveAs下载文件
      const dataBlob = new Blob([excelBuffer], {
        type:
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"
      });
      saveAs(dataBlob, "export.xlsx");
    },
    checkFn(item) {
      this.$confirm("确定将此用户设置为已付款状态吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let data = {
          user_bill_id: item.user_bill_id
        };
        const loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(255, 255, 255, 0.7)"
        });
        checkStatus(data)
          .then(res => {
            if (res.data.code == 1) {
              //更改状态成功
              this.$message({
                type: "success",
                message: res.data.msg
              });
              this.getData();
            } else {
              this.$message(res.data.msg);
            }
            loading.close();
          })
          .catch(err => {
            loading.close();
            this.$message("服务器请求超时");
          });
      });
    }
  },
  mounted() {},
  created() {
    this.getHeadData();
    this.getData();
  }
};
</script>

<style scoped lang="less">
.container {
  width: 100%;
  display: flex;
  overflow: hidden;
  .righCont {
    display: flex;
    flex-direction: column;
    flex: 1;
    margin-left: 15px;
    height: 100%;
    width: 10px;
    background-color: #f4f8ff;
    .cardCont {
      padding: 15px 20px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      .carItem {
        width: 28%;
        background-color: #fff;
        border-radius: 5px;
        padding: 15px;
        box-sizing: border-box;
        .carName {
        }
        .carText {
          margin-top: 5px;
          font-weight: bold;
          font-size: 20px;
        }
      }
    }
    .content {
      padding: 20px;
      padding-top: 0px;
      box-sizing: border-box;
      width: 100%;
      flex: 1;
      overflow: auto;
      .payTable {
        width: 100%;
        background-color: #fff;
        padding: 15px;
        box-sizing: border-box;
        .trendTop {
          font-weight: bold;
          font-size: 16px;
          cursor: pointer;
        }
        .tableCont {
          width: 100%;
          margin-top: 15px;
          .tableSerchCont {
            width: 100%;
            display: flex;
            justify-content: flex-end;
            margin-bottom: 10px;
            .serchItem {
              margin-left: 10px;
            }
          }
          .ctrolBtn {
            display: flex;
            .text_btn {
              cursor: pointer;
              font-size: 14px;
            }
            .balckBtn {
              color: #666;
            }
            .redBtn {
              color: #f56c6c;
            }
            .greenBtn {
              color: #85ce61;
            }
            .blueBtn {
              color: #96a1fe;
            }

            .hasRight {
              margin-right: 10px;
            }
          }
          .pageCont {
            padding: 30px 0;
            display: flex;
            justify-content: center;
          }
        }
      }
    }
  }
  .formInfo {
    display: flex;
    flex-wrap: wrap;
    .formItem {
      width: 48%;
      margin-bottom: 15px;
      display: flex;
      align-items: center;
      .formName {
        width: 80px;
        text-align: right;
        margin-right: 15px;
      }
      .redBtn {
        color: #f56c6c;
      }
      .greenBtn {
        color: #85ce61;
      }
    }
  }
}
</style>
